package com.share.PACManager.task;

import java.net.URLEncoder;
import java.util.Map;
import java.util.TreeMap;

import org.json.JSONException;
import org.json.JSONObject;

import com.share.PACManager.model.TerminalModel;
import com.share.PACManager.net.HTTPResponse;
import com.share.PACManager.net.HttpUtility;
import com.share.PACManager.utils.Cryptography;
import com.share.PACManager.utils.Utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;

public class CheckPasswordTask extends Thread{

	private CheckPasswordListener mListener;
	private Context mContext;
	private String mPassword;
	private TerminalModel mTerminal;
	private boolean bLocalEqual;
	
	public interface CheckPasswordListener {
		public String requestPassword();
		public TerminalModel requestTerminal();
		public void onCheckResult(boolean result, boolean bLocalEqual);
	}

	public CheckPasswordTask(Context context) {
		mContext = context;
	}
	
	public void start(CheckPasswordListener listener) {
		mListener = listener;	
		if(mListener!= null)
		{
			mTerminal = mListener.requestTerminal();
			start();
		}
	}

	Handler handler = new Handler(){		
		@Override
		public void handleMessage(Message msg)
		{
			if(mListener != null)
			{
				if(msg.what != 0)
					mListener.onCheckResult(false, bLocalEqual);
				else
				{
					HTTPResponse resp = (HTTPResponse) msg.obj;
					if(resp.code == 200)
					{
						if(!parse(resp.content))
							mListener.onCheckResult(false, false);
						else
						{
							mListener.onCheckResult(true, bLocalEqual);
						}	
					}
					else
						mListener.onCheckResult(false, bLocalEqual);
				}
			}
		}
	};
	
    public boolean parse(String json)
    {
		try {
			JSONObject jsonObject = new JSONObject(json);
			if(!jsonObject.getBoolean("Success"))
				return false;
			
			JSONObject state = jsonObject.getJSONObject("State"); 
			if(state != null)
			{
				mTerminal.TerminalId = Utils.getJsonString(state, "TerminalId");
				mTerminal.TerminalCode = Utils.getJsonString(state, "TerminalCode");
				mTerminal.TerminalPassword = Utils.getJsonString(state, "TerminalPassword");
				mTerminal.TerminalPhoneNumber = Utils.getJsonString(state, "TerminalPhoneNumber");
				mTerminal.TerminalType = Utils.getJsonString(state, "TerminalType");
				mTerminal.TerminalStatus = Utils.getJsonString(state, "TerminalStatus");
				mTerminal.TerminalArea = Utils.getJsonString(state, "TerminalArea");
				mTerminal.TerminalCreateTime = Utils.getJsonString(state, "TerminalCreateTime");
				mTerminal.TerminalChangeTime = Utils.getJsonString(state, "TerminalChangeTime");
				mTerminal.TerminalComment = Utils.getJsonString(state, "TerminalComment");
			}
			bLocalEqual = mPassword.equals(mTerminal.TerminalPassword);
			return true;			
			
		} catch (JSONException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
    	return false;
    }
    
	@Override
	public void run() {

		try {
			mPassword = Cryptography.hashPassword(mListener.requestPassword());
			bLocalEqual = mPassword.equals(mTerminal.TerminalPassword);
			
			String terminalId = "Guest";
			String imei = Utils.getImei(mContext);
			String password = Cryptography.hashPassword("Password");
			String ticket = Cryptography.AES_Encode("IMEI", terminalId, password);
			String info = Cryptography.AES_Encode("IMEI=" + imei, terminalId, password);

			Map<String, String> params = new TreeMap<String, String>();
			params.put("Id", terminalId);
			params.put("DataType", "Data");	
			params.put("Password", password);
			params.put("TerminalCode", "IMEI");
			params.put("Version", "1.0");
			params.put("Ticket", ticket);
			params.put("Objective", "GetTerminal");
			
			String entity = "Information=" + URLEncoder.encode(info);			
			handler.obtainMessage(0, HttpUtility.httpPost(params, entity)).sendToTarget();

		} catch (Exception e) {
			handler.obtainMessage(1).sendToTarget();
		}
		
		super.run();
	}
}
